import { Body, Controller, Get, Post, Res, UseInterceptors, Request, Query } from '@nestjs/common';
import utils from 'src/common/utils';
import { ValidRSAInterceptor } from 'src/interceptor/validRSA';
import { ValidTokenInterceptor } from 'src/interceptor/validToken';
import { apibefore } from 'src/router';
import { Notices } from './notices.entity';
import { NoticeCheckEnum, NoticeIsDeleteEnum, NoticeTypeEnum } from './notices.enum';
import { NoticesService } from './notices.service';

@UseInterceptors(ValidRSAInterceptor)
@UseInterceptors(ValidTokenInterceptor)
@Controller(apibefore('/notices'))
export class NoticesController {
  constructor(private readonly service: NoticesService) {}

  @Post('/readNotices')
  async readNotices(@Body() data){
    return await this.service.readNotices(data.ids)
  }

  @Get('/getNotRead')
  async getNotRead(@Request() req){
    return await this.service.getNotRead(req.headers.wx_user)
  }

  @Get('/findAll')
  async findAll(@Query() params, @Request() req){
    let {
      take, skip
    } = utils.getLimit(params.pageSize, params.pageIndex)
    let dataList = await this.service.findAll_By_WxUser(req.headers.wx_user,take, skip)
    let returnResult = []
    dataList.map((e:any) => {
      let item = {
        id:e.id,
        video_id:e.video_id,
        create_time:new Date(e.create_time).getTime(),
        notice:'',
        is_read: e.is_read,
        videoType:0,
        is_delete: e.is_delete === NoticeIsDeleteEnum.yes
      }
      switch(e.type){
        case NoticeTypeEnum.answer_accept:
          item.notice = '您的答案已通过审核'
          item.videoType = 4
          break
        case NoticeTypeEnum.answer_reject:
          // item.notice = '出处不正确，回答已驳回'
          item.videoType = 4
          break
        // case NoticeTypeEnum.answer_exists:
        //   item.notice = '该出处已找到，回答已驳回'
        //   item.videoType = 4
        //   break;
        case NoticeTypeEnum.collection_is_answer:
          item.notice = '您收藏的内容已有答案'
          item.videoType = 2
          break
        case NoticeTypeEnum.question_accept:
          item.notice = '您的提问已通过审核'
          item.videoType = 1
          break
        case NoticeTypeEnum.question_reject:
          item.notice = '您的提问已被驳回'
          item.videoType = 1
          break
        case NoticeTypeEnum.question_is_answer:
          item.notice = '您的提问已有答案'
          item.videoType = 1
          break
        case NoticeTypeEnum.works_accept:
          item.notice = '您的作品已通过审核'
          item.videoType = 3
          break
        case NoticeTypeEnum.works_reject:
          item.notice = '您的作品已被驳回'
          item.videoType = 3
          break
        case NoticeTypeEnum.works_delete:
          item.notice = '您的作品已被删除'
          item.videoType = 3
          break
        case NoticeTypeEnum.refund_accept:
          item.notice = '您的退款申请已通过'
          item.videoType = 2
          break
        case NoticeTypeEnum.refund_reject:
          item.notice = '您的退款申请未通过'
          item.videoType = 2
          break
        case NoticeTypeEnum.refund_answer_is_error_accept:
          item.notice = '您的退款已自动发起'
          item.videoType = 1
          break;
      }
      if(e.reason){
        item.notice = (item.notice ? item.notice + '，' : '') + e.reason
      }
      returnResult.push(item)
    })
    return returnResult
  }
}
